package com.wulinbus.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import com.fancy.common.utils.FileUtil;
import com.fancy.common.utils.StringUtil;

public class iii {

	public static int count = 0;

	// public static void forEachFile(String parentName, String dirpath) {
	// File dir = new File(dirpath);
	// if (dir.exists()) {
	// File[] files = dir.listFiles();
	// if (files.length > 0) {
	// for (File f : files) {
	// if (f.isDirectory()) {
	// System.err.println("目录:" + f.getName());
	// forEachFile(f.getName(), f.getAbsolutePath());
	// } else {
	// System.err.println("文件:" + parentName + "==" + f.getName());
	// }
	// }
	// }
	// }
	// }

	// public static void forEachFile(String parentName, String dirpath) {
	// File dir = new File(dirpath);
	// if (dir.exists()) {
	// File[] files = dir.listFiles();
	// for (int i = 0; i < files.length; i++) {
	// if (files[i].isDirectory()) {
	// forEachFile(files[i].getName(), files[i].getAbsolutePath());
	// } else {
	// if (i == 0) {
	// System.err.println("文件0:" + parentName + "/" + files[i].getName());
	// } else {
	// System.err.println("文件:" + parentName + "/" + files[i].getName());
	// }
	// }
	// }
	// }
	// }

	public static void forEachFile(String parentName, String dirpath) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					if (files[i].getName().startsWith(".")) {
						files[i].delete();
						System.err.println("目录del:" + parentName + "/" + files[i].getName());
					}
					forEachFile(parentName + "/" + files[i].getName(), files[i].getAbsolutePath());
					if (StringUtil.isEmpty(parentName)) {
						count = 0;
					}
				} else {

					if (files[i].getName().startsWith(".") || files[i].getName().endsWith(".db")) {
						files[i].delete();
						System.err.println("文件del:" + parentName + "/" + files[i].getName());
					} else {
						if (parentName.lastIndexOf("/") > 0) {
							count++;
							String newFile = "F:/角色2" + parentName.substring(0, parentName.lastIndexOf("/")) + "/" + count + ".png";
							System.err.println("newFile:" + newFile);
							FileUtil.copyFile(files[i].getAbsolutePath(), newFile, true);
							System.err.println("文件:" + parentName + "/" + files[i].getName());
						} else {
							System.err.println("文件0:" + parentName + "/" + files[i].getName());
						}
					}
				}
			}
		}
	}

	public static ArrayList<File> delFiles = new ArrayList<File>();

	public static void delDirecory(String dirpath, int level) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					if (level > 0) {
						// boolean dd = files[i].delete();
						delFiles.add(files[i]);
						// System.err.println(files[i].getAbsolutePath() + dd);
					}
					delDirecory(files[i].getAbsolutePath(), level + 1);
					if (level == 0) {
						System.err.println("=====");
					}
				}
			}
		}
	}

	public static void forEachFile2(String parentName, String dirpath) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					forEachFile2(parentName + "/" + files[i].getName(), files[i].getAbsolutePath());
				} else {
					if (parentName.lastIndexOf("/") > 0) {
					} else {
						if (files[i].getName().endsWith(".png")) {
							files[i].delete();
							System.err.println("文件:" + files[i].getName());
						}
					}
					// files[i].delete();
				}
			}
		}
	}

	public static int fileCount = 0;

	public static void reName(String dirpath, int level) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (files[i].isDirectory()) {
					reName(file.getAbsolutePath(), level + 1);
					if (level == 0) {
						fileCount = 0;
					}
				} else {
					if (file.getName().endsWith(".png")) {
						fileCount++;
						File parentFile = file.getParentFile();
						File newFile = new File(parentFile, String.valueOf(fileCount) + ".png");
						file.renameTo(newFile);
					}
				}
			}
		}
	}

	public static void readIdea(String parentName, String dirpath) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					readIdea(parentName + "/" + files[i].getName(), files[i].getAbsolutePath());
					if (StringUtil.isEmpty(parentName)) {
						System.err.println("======================");
					}
				} else {
					System.err.println("文件:" + parentName + "/" + files[i].getName());
				}
			}
		}
	}

	public static void rePinyinName(String dirpath, int level) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (file.isDirectory()) {
					//
					rePinyinName(file.getAbsolutePath(), level + 1);
					//
					File parentFile = file.getParentFile();
					File newFile = new File(parentFile, Trans2PinYin.trans2PinYin(file.getName()));
					file.renameTo(newFile);
				} else {
					File parentFile = file.getParentFile();
					File newFile = new File(parentFile, Trans2PinYin.trans2PinYin(file.getName()));
					file.renameTo(newFile);
				}
			}
		}
	}

	public static void delMoreFile(String dirpath, int level) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (file.isDirectory()) {
					//
					delMoreFile(file.getAbsolutePath(), level + 1);
					//
				} else {
					if (!file.getName().equals("1.png")) {
						file.delete();
					}
				}
			}
		}
	}

	public static void reNameCoreFile(String dirpath, int level) {
		File dir = new File(dirpath);
		if (dir.exists()) {
			File[] files = dir.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (file.isDirectory()) {
					//
					reNameCoreFile(file.getAbsolutePath(), level + 1);
					//
				} else {
					File parentFile = file.getParentFile();
					File newFile = new File(parentFile, "s1.png");
					file.renameTo(newFile);
				}
			}
		}
	}
	
	public static void main(String[] args) {
		String dirpath = FileUtil.getFilePath("F:/创意(含obj) - 副本3/景物");
		reNameCoreFile(dirpath,0);
		// rePinyinName(dirpath, 0);
		// readIdea("", dirpath);
		// reName(dirpath, 0);
		// forEachFile("", dirpath);
		// delDirecory(dirpath, 0);
		//
		// for (File file : delFiles) {
		// System.err.println(file.exists());
		// System.err.println(file.getAbsoluteFile().exists());
		// System.err.println(file.delete());
		//
		// }
	}
	// F:\道具

}
